package com.chinaisafe.user.service.impl;

import com.chinaisafe.user.entity.SysLoginLog;
import com.chinaisafe.user.mapper.SysLoginLogMapper;
import com.chinaisafe.user.service.SysLoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.user.vo.request.SysLoginLogReqVO;
import com.chinaisafe.user.vo.response.SysLoginLogRespVO;
import org.springframework.stereotype.Service;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.constant.MybatisplusConstants;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;

import java.util.Arrays;
import java.util.Optional;
/**
 * SysLoginLogServiceImpl
 * Description：登录日志服务实现类
 *
 * @author xuefu.shu
 * @date 2024-07-12 09:55:54
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Override
    public IPage<SysLoginLogRespVO> getPageList(SysLoginLogReqVO sysLoginLogReqVO) {
        // 分页设置
        Page<SysLoginLog> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysLoginLogReqVO);
        // 查询条件构造
        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysLoginLog::getDeleted, MybatisplusConstants.LOGIC_NOT_DELETE_VALUE);
        return baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysLoginLogRespVO add(SysLoginLogReqVO sysLoginLogReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysLoginLogReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysLoginLogRespVO update(SysLoginLogReqVO sysLoginLogReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysLoginLogReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysLoginLogRespVO addOrUpdate(SysLoginLogReqVO sysLoginLogReqVO) {
        Long sysLoginLogId = sysLoginLogReqVO.getId();
        if (sysLoginLogId != null){
            // 编辑
            SysLoginLog sysLoginLog = baseMapper.selectById(sysLoginLogId);
            Optional.ofNullable(sysLoginLog).orElseThrow(() -> BizException.build("未找到要修改的登录日志信息"));
        }
        SysLoginLog sysLoginLog2Save = sysLoginLogReqVO.convertToEntity();
        boolean success = this.saveOrUpdate(sysLoginLog2Save);
        if (success) {
            return SysLoginLogRespVO.convertFromEntity(sysLoginLog2Save);
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public SysLoginLogRespVO getDetailById(Long sysLoginLogId) {
        SysLoginLog sysLoginLog = baseMapper.selectById(sysLoginLogId);
        Optional.ofNullable(sysLoginLog).orElseThrow(() -> BizException.build("未找到对应登录日志信息"));
        return SysLoginLogRespVO.convertFromEntity(sysLoginLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] sysLoginLogIds) {
        if (sysLoginLogIds.length > 0) {
            this.removeByIds(Arrays.asList(sysLoginLogIds));
        }
    }
}
